package be.vdab.servlets;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.vdab.dao.GenreDAO;
import be.vdab.dao.VoorstellingDAO;
import be.vdab.entities.Genre;
import be.vdab.entities.Voorstelling;

/**
 * Servlet implementation class WelkomServlet
 */
@WebServlet("/welkom.htm")
public class WelkomServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static String VIEW = "/WEB-INF/JSP/welkom.jsp";
	public static String REDIRECT_VIEW = "/welkom.htm";
	public VoorstellingDAO voorstellingDAO = VoorstellingDAO.getInstance();
	public GenreDAO genreDAO = new GenreDAO();
	
	

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Map<Long, Long> mandje = null;
		HttpSession session = null;
		session = request.getSession();	
		if (session != null) {
			mandje = (Map<Long, Long>) session.getAttribute("mandje");
			if (mandje != null) {
				request.setAttribute("mandje", mandje);
			}
		}
		List<Genre> genres = genreDAO.zoekGenre();
		request.setAttribute("genres", genres);

		if (request.getParameter("genrenummer") != null) {
			Long nummer = 0L;
			nummer = Long.parseLong(request.getParameter("genrenummer"));
			String genrenaam = genreDAO.genreNaam(nummer);
			List<Voorstelling> voorstellingen = voorstellingDAO
					.zoekVoorstellingen(nummer);
			request.setAttribute("voorstellingen", voorstellingen);
			request.setAttribute("genrenaam", genrenaam);
		}

		request.getRequestDispatcher(VIEW).forward(request, response);

	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher(VIEW).forward(request, response);
	}
}
